package cn.bocweb.spinning.view.forun;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ForumListBean;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.activity.ImgDetailActivity;
import cn.bocweb.spinning.view.common.GlideCircle;
import cn.bocweb.spinning.view.common.MyAdapter;

/**
 * Created by chenchangxing on 16/3/11.
 */
public class ForunSpecialAdapter extends MyAdapter<ForumListBean.DataBean> {

    public ForunSpecialAdapter(Context context, List<ForumListBean.DataBean> list) {
        super(context, list);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_forun_special_img, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Glide.with(context)
                .load(list.get(position).getUthumb())
                .placeholder(R.mipmap.drawer_icon_unlogin)
                .transform(new GlideCircle(context))
                .into(holder.bbsSpecialPicHead);
        holder.bbsSpecialPicName.setText(list.get(position).getNickname());
        holder.bbsSpecialPicTitle.setText(list.get(position).getTitle());
        holder.bbsSpecialPicContent.setText(list.get(position).getContent());
        holder.bbsSpecialPicReply.setText(list.get(position).getCount());
        holder.textViewLike.setText(list.get(position).getUpnum());
        String time = DeviceUtil.getTimeByTwoTimeStamp(Long.parseLong(list.get(position).getTimeline()) * 1000, System.currentTimeMillis());
        holder.bbsSpecialPicTime.setText(time);
        int childNum = 0;

        if (list.get(position).getPhoto() == null || list.get(position).getPhoto().isEmpty()) {
            holder.bbsSpecialPicImg.setVisibility(View.GONE);
        } else {
            for (int i = 0; i < holder.bbsSpecialPicImg.getChildCount(); i++) {
                holder.bbsSpecialPicImg.getChildAt(i).setVisibility(View.INVISIBLE);
            }
            holder.bbsSpecialPicImg.setVisibility(View.VISIBLE);
            for (int i = 0; i < list.get(position).getPhoto().size(); i++) {
                holder.bbsSpecialPicImg.getChildAt(childNum).setVisibility(View.VISIBLE);
                Glide
                        .with(context)
                        .load(list.get(position).getPhoto().get(i))
                        .placeholder(R.mipmap.banner_empty)
                        .into((ImageView) holder.bbsSpecialPicImg.getChildAt(childNum));
                holder.bbsSpecialPicImg.getChildAt(childNum).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(parent.getContext(), ImgDetailActivity.class);
                        intent.putStringArrayListExtra(Constant.IMG_DETAIL_URL, (ArrayList<String>) list.get(position).getPhoto());
                        parent.getContext().startActivity(intent);
                    }
                });
                childNum += 2;
            }
        }

        return convertView;
    }

    class ViewHolder {
        @Bind(R.id.bbs_special_pic_head)
        ImageView bbsSpecialPicHead;
        @Bind(R.id.bbs_special_pic_name)
        TextView bbsSpecialPicName;
        @Bind(R.id.bbs_special_pic_title)
        TextView bbsSpecialPicTitle;
        @Bind(R.id.bbs_special_pic_content)
        TextView bbsSpecialPicContent;
        @Bind(R.id.bbs_special_pic_img)
        LinearLayout bbsSpecialPicImg;
        @Bind(R.id.bbs_special_pic_reply)
        TextView bbsSpecialPicReply;
        @Bind(R.id.bbs_special_pic_time)
        TextView bbsSpecialPicTime;
        @Bind(R.id.textview_like)
        TextView textViewLike;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
